﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;

namespace SchoolManagementSystem.Searchs
{
    public partial class ProductSearchUC : SearchUC
    {
        public ProductSearchUC()
        {
            InitializeComponent();
            LoadProductGroup();
            LoadProvider();
        }
        void LoadProductGroup()
        {
            try
            {
                var data = (List<Product_Group>)RegisterService.RegisterServices.ProductGroupService.GetList();
                data.Insert(0, new Product_Group()
                {
                    Active = true,
                    Code = "Tất cả",
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_ProductGroup.Properties.DataSource = data.OrderBy(c => c.ID);
                lookUpEdit_ProductGroup.Properties.ValueMember = "Code";
                lookUpEdit_ProductGroup.Properties.DisplayMember = "Code";
                string[] columns = { "Code" };
                for (int i = 0; i < lookUpEdit_ProductGroup.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_ProductGroup.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_ProductGroup.Properties.Columns[i].Visible = false;
                            lookUpEdit_ProductGroup.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }
        void LoadProvider()
        {
            try
            {
                var data = (List<Product_Provider>)RegisterService.RegisterServices.ProductProviderService.GetList();
                data.Insert(0, new Product_Provider()
                {
                    Active = true,
                    Code = "Tất cả",
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_Provider.Properties.DataSource = data.OrderBy(c => c.ID);
                lookUpEdit_Provider.Properties.ValueMember = "Code";
                lookUpEdit_Provider.Properties.DisplayMember = "Code";
                string[] columns = { "Code" };
                for (int i = 0; i < lookUpEdit_Provider.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_Provider.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_Provider.Properties.Columns[i].Visible = false;
                            lookUpEdit_Provider.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }

        private void cmdclear_Click(object sender, EventArgs e)
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtPriceFrom.EditValue = 0;
            txtPriceTo.EditValue = 0;
            lookUpEdit_ProductGroup.EditValue = 0;
            lookUpEdit_Provider.EditValue = 0;
        }

        private void cmdtim_Click(object sender, EventArgs e)
        {
            decimal priceFrom = Convert.ToDecimal(txtPriceFrom.EditValue);
            decimal priceTo = Convert.ToDecimal(txtPriceTo.EditValue);
            string textcode = txtCode.Text;
            string textname = txtName.Text;
            int groupid = Convert.ToInt16(lookUpEdit_ProductGroup.EditValue);
            int providerid = Convert.ToInt16(lookUpEdit_Provider.EditValue);
            string searchtext="";
            if(!string.IsNullOrEmpty(textcode))
                searchtext += " Product.Code like %" + textcode+"% and";
            if (!string.IsNullOrEmpty(textname))
                searchtext += " Product.Name like %" + textcode + "% and";
            if (groupid > 0)
                searchtext += " Product_Group.ID=" + groupid +" and";
            if (providerid > 0)
                searchtext += " Product_Provider.ID=" + groupid + " and";
            if(priceTo>0)
                searchtext += " Product.InputPrice >=" + priceFrom + "and Product.InputPrice<=" + priceTo + " and";
            if (!string.IsNullOrEmpty(searchtext))
            {
                searchtext = searchtext.Remove(searchtext.Length - 3, 3);
                Master.SearchString = searchtext;
            }
            else
            Master.SearchString = "1=1";

        }

        private void txtPriceTo_EditValueChanged(object sender, EventArgs e)
        {
            ValidatePrice();
        }

        private void txtPriceFrom_EditValueChanged(object sender, EventArgs e)
        {
            ValidatePrice();
        }
        void ValidatePrice()
        {
            decimal priceFrom = Convert.ToDecimal(txtPriceFrom.EditValue);
            decimal priceTo = Convert.ToDecimal(txtPriceTo.EditValue);
            if (priceTo < priceFrom)
            {
                txtPriceTo.EditValue = priceFrom;
                MessageBox.Show("Khoảng giá đến thấp hơn từ", "Nhập liệu không đúng", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void simpleButton_close_Click(object sender, EventArgs e)
        {
            ClosePanel();
        }
    }
}
